//
//  ZDBRankingViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/14.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBRankingViewController.h"
#import "ZDBRankListTableViewCell.h"
#import "ZDBRankListInfoView.h"
#import "ZDBRankListHeaderView.h"
#import "ZDBRankListModel.h"
@interface ZDBRankingViewController ()
<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
@property (nonatomic, strong) UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * subDataArry;
@property(nonatomic,strong)NSMutableArray * rankInfoDataArry;
@property (nonatomic, strong) ZDBRankListInfoView * my_infoView;
@property (nonatomic, strong) ZDBRankListModel * my_infoModel;
@property (nonatomic,strong) ZDBRankListHeaderView * tableviewHeadView;
@end

@implementation ZDBRankingViewController

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.my_infoView.frame = CGRectMake(0, UIScreenHeight-TABBAR_HEIGHT-25-60-kStatusBarAndNavigationBarHeight, UIScreenWidth, 60);
    self.tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 220);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航控制器的代理为self
    self.navigationController.delegate = self;
    // Do any additional setup after loading the view.
  
    self.modelsArry = [NSMutableArray array];
    self.subDataArry = [NSMutableArray array];
    self.rankInfoDataArry = [NSMutableArray array];
    [self creatUI];
    
    
    // 上拉加载
    WEAKSELF
      MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
          STRONGSELFFor(weakSelf)
          [strongSelf getRankList];
          [strongSelf getRankingInfo];
      }];
      [header beginRefreshing];
      self.tableView.mj_header = header;
    
    
       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LoginSusess) name:LoginSusessNotificationse object:nil];
       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LogOutSusess) name:ExitLogonNotificationse object:nil];
       
    
  
    
    
    
}
-(void)LoginSusess{
    
    [self.tableView.mj_header beginRefreshing];
}
-(void)LogOutSusess{
    self.my_infoView.hidden = YES;
    [self.tableView.mj_header beginRefreshing];
}

-(void)getRankingInfo{
    
    NSString  * inurl =@"";
    if (self.showType ==1) {
       inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getWeekRanking"];
    }else{
      inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getMonthRanking"];
    }
   WEAKSELF
    [[SmileHttpTool sharedInstance] POST :inurl parameters:nil origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
            STRONGSELFFor(weakSelf)
              
              if (statusCode==0) {
                  [strongSelf.rankInfoDataArry removeAllObjects];
                  NSArray * dataArry = [NSArray arrayWithArray:responseObject];
                  strongSelf.rankInfoDataArry = [NSMutableArray arrayWithArray:dataArry];
              }else{
                   [JMNotifyView showNotify:message isSuccessful:NO];
              }
        [strongSelf.tableviewHeadView refushRankInfoData:strongSelf.rankInfoDataArry];
          } failure:^(id responseObject, NSError *error) {
            //  NSLog(@"error----%@",error);
             //  STRONGSELFFor(weakSelf)
              //  [strongSelf.tableView.mj_header endRefreshing];
              [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
              // [self stopLoadingAnimation];
          }];

}
-(void)getRankList{
    
    NSString  * inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"rankingList_getList"];
    NSMutableDictionary * dataDicc = [NSMutableDictionary dictionary];
    [dataDicc setValue:@(self.showType) forKey:@"type"];
         WEAKSELF
         [[SmileHttpTool sharedInstance] POST :inurl parameters:dataDicc origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
           STRONGSELFFor(weakSelf)
             
             if (statusCode==0) {
            [strongSelf.modelsArry  removeAllObjects];
            [strongSelf.subDataArry  removeAllObjects];
            NSLog(@"rankingList_getList----%@",responseObject);
            NSDictionary * datadiccfrist = [NSDictionary dictionaryWithDictionary:responseObject];
            NSDictionary * datadicc =[datadiccfrist jsonDict:@"userInfoPageInfo"];
            NSDictionary * userInfodicc =[datadiccfrist jsonDict:@"userInfo"];
            if (kDictIsEmpty(userInfodicc) || userInfodicc.allKeys ==0) {
              
                strongSelf.my_infoView.hidden = YES;
            }else{
                strongSelf.my_infoModel = [[ZDBRankListModel alloc]initWithDictionary:userInfodicc error:nil];
                strongSelf.my_infoView.hidden = NO;
                
            }
                 NSMutableArray * dataArry =[datadicc  jsonArray:@"list"].mutableCopy;
                 
                 if (dataArry.count>3) {
                     //数据多余3条
                     NSArray * subarr = [dataArry subarrayWithRange:NSMakeRange(0, 3)];
                     strongSelf.subDataArry = [ZDBRankListModel arrayOfModelsFromDictionaries:subarr error:nil];
                     [dataArry removeObjectsInArray:subarr];
                     NSArray * subarr2 = dataArry;
                     //[dataArry subarrayWithRange:NSMakeRange(strongSelf.subDataArry.count, dataArry.count)];
                     strongSelf.modelsArry = [ZDBRankListModel arrayOfModelsFromDictionaries:subarr2 error:nil];
                     [strongSelf.my_infoView refushUserInfoModel:strongSelf.my_infoModel andArry:strongSelf.modelsArry];
                 }else{
                     //数据小于3
                     strongSelf.subDataArry = [ZDBRankListModel arrayOfModelsFromDictionaries:[datadicc jsonArray:@"list"] error:nil];
                     [strongSelf.my_infoView refushUserInfoModel:strongSelf.my_infoModel andArry:strongSelf.subDataArry];
                 }
             }else{
                  [JMNotifyView showNotify:message isSuccessful:NO];
             }
             
             [strongSelf.tableviewHeadView  refushData:strongSelf.subDataArry];
            
             // [strongSelf stopLoadingAnimation];
             [strongSelf.tableView reloadData];
             [strongSelf.tableView.mj_header endRefreshing];
         } failure:^(id responseObject, NSError *error) {
             NSLog(@"error----%@",error);
              STRONGSELFFor(weakSelf)
               [strongSelf.tableView.mj_header endRefreshing];
             [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
             // [self stopLoadingAnimation];
         }];

}


-(void)creatUI{
   
    self.view.backgroundColor =  RGBA(249, 249, 249, 1);
    [self.view addSubview:self.tableView];
    
    self.tableviewHeadView =[[NSBundle mainBundle] loadNibNamed:@"ZDBRankListHeaderView" owner:self options:nil].lastObject;
    self.tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 220);
    self.tableView.tableHeaderView =self.tableviewHeadView;
    
    self.my_infoView = [[NSBundle mainBundle] loadNibNamed:@"ZDBRankListInfoView" owner:self options:nil].lastObject;
    self.my_infoView.frame = CGRectMake(0, UIScreenHeight-TABBAR_HEIGHT-25-60-kStatusBarAndNavigationBarHeight, UIScreenWidth, 60);
    self.my_infoView.centerY.constant = -10;
    self.my_infoView.tishiLab.hidden = NO;
    self.my_infoView.bgImage.hidden = NO;
    self.my_infoView.bgView.backgroundColor = [UIColor clearColor];
    self.my_infoView.bgImage.backgroundColor = [UIColor whiteColor];
    self.my_infoView.bgImage.layer.shadowOffset = CGSizeZero;//默认为0,-3
    self.my_infoView.bgImage.layer.shadowColor = [UIColor colorWithHexString:@"#aeaeae "].CGColor;
    self.my_infoView.bgImage.layer.shadowOpacity = 0.35;//阴影透明度，默认0
    self.my_infoView.bgImage.layer.cornerRadius = 5;
    self.my_infoView.hidden = YES;
   // KViewRadius(self.my_infoView.bgView, 5);
    [self.view addSubview:self.my_infoView];
    if (self.showType ==1) {
        self.tableviewHeadView.tishiLab.text = @"周榜是每周一0:00结算";
    }else{
        self.tableviewHeadView.tishiLab.text = @"月榜是每月1号0:00结算";
    }
  
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelsArry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBRankListTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBRankListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ZDBRankListModel * modell = self.modelsArry[indexPath.row];
    cell.infoView.nameLab.text = modell.nikeName;
    cell.infoView.jineLab.text = [NSString stringWithFormat:@"¥%ld",[modell.money integerValue]];
    cell.infoView.numLab.text = [NSString stringWithFormat:@"%ld",indexPath.row+4];
    [cell.infoView.headerImage sd_setImageWithURL:[NSURL URLWithString:modell.headImage] placeholderImage:PlaceHolderImage];
    
    // KViewRadius(cell.infoView, 5);
    return cell;
  
}


#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight-kStatusBarAndNavigationBarHeight-TABBAR_HEIGHT)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  RGBA(249, 249, 249, 1);
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBRankListTableViewCell" bundle:nil]  forCellReuseIdentifier:@"ZDBRankListTableViewCell"];
        //_tableView.tableHeaderView =self.tableviewHeadView;
    }
    return _tableView;
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
 // 判断要显示的控制器是否是自己
 BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
 
 [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO]; //设置隐藏
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:NO]; //设置隐藏
    [super viewWillDisappear:animated];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
